<!DOCTYPE html>
<html lang="zh-cn">

<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	
	<title>划水王  | 基础结构</title>
	<meta name="viewport" content="width=device-width,minimum-scale=1">
	<meta name="generator" content="Hugo 0.74.3" />
	
	
	<META NAME="ROBOTS" CONTENT="INDEX, FOLLOW">
	

	
	
	<link href="/dist/app.css" rel="stylesheet">
	

	

	
	
	

	
	
	
	
	
	



<link rel="stylesheet" href='https://flwwsg.gitee.io/lib/katex.min.css' integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq" crossorigin="anonymous">


<script defer src='https://flwwsg.gitee.io/lib/katex.min.js' integrity="sha384-y23I5Q6l+B6vatafAwxRu/0oK/79VlbSz7Q9aiSZUvyWYIYsd+qj+o24G5ZU2zJz" crossorigin="anonymous"></script>


<script defer src='https://flwwsg.gitee.io/lib/contrib/auto-render.min.js' integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI"
crossorigin="anonymous"
onload='renderMathInElement(document.body);'></script>


<script>
    document.addEventListener("DOMContentLoaded", function() {
        renderMathInElement(document.body, {
            delimiters: [
                {left: "$$", right: "$$", display: true},
                {left: "$", right: "$", display: false}
            ]
        });
    });
</script>

	
	
	
	<script>
		(function (u, c) {
			var d = document,
				t = 'script',
				o = d.createElement(t),
				s = d.getElementsByTagName(t)[0];
			o.src = u;
			if (c) {
				o.addEventListener('load', function (e) {
					c(e);
				});
			}
			s.parentNode.insertBefore(o, s);
		})('https:\/\/flwwsg.gitee.io\/lib\/pangu.min.js', function () {
			pangu.spacingPage();
		});
	</script>
	
	
	<style type="text/css" media="screen, print">
		@font-face {
			font-family: "FancyTitleFont";
			font-style: normal;
			font-display: swap;
			src: url('%25!s%28%3cnil%3e%29.woff2') format('woff2'),
				url('%25!s%28%3cnil%3e%29.woff') format('woff');
		}

		 
		@font-face {
			font-family: 'Noto Serif CJK SC';
			font-style: normal;
			font-weight: 300;
			font-display: swap;
			src: local('Noto Serif CJK SC Light'), local('NotoSerifCJK-Light'),
				url('https://flwwsg.gitee.io/fonts/noto-serif-sc-v7-latin_chinese-simplified-300.woff2') format('woff2'),
				 
				url('https://flwwsg.gitee.io/fonts/noto-serif-sc-v7-latin_chinese-simplified-300.woff') format('woff');
			 
		}

		 
		@font-face {
			font-family: 'Noto Serif CJK SC';
			font-style: normal;
			font-weight: 400;
			font-display: swap;
			src: local('Noto Serif CJK SC'), local('NotoSerifCJK-Regular'),
				url('https://flwwsg.gitee.io/fonts/noto-serif-sc-v7-latin_chinese-simplified-regular.woff2') format('woff2'),
				 
				url('https://flwwsg.gitee.io/fonts/noto-serif-sc-v7-latin_chinese-simplified-regular.woff') format('woff');
			 
		}

		 
		@font-face {
			font-family: 'Noto Serif CJK SC';
			font-style: normal;
			font-weight: 500;
			font-display: swap;
			src: local('Noto Serif CJK SC Medium'), local('NotoSerifCJK-Medium'),
				url('https://flwwsg.gitee.io/fonts/noto-serif-sc-v7-latin_chinese-simplified-500.woff2') format('woff2'),
				 
				url('https://flwwsg.gitee.io/fonts/noto-serif-sc-v7-latin_chinese-simplified-500.woff') format('woff');
			 
		}
	</style>
	
	
</head>

<body class="bg-gray-100 text-gray-700 font-sans">
	<div class="p-6 sm:p-10 md:p-16 flex flex-wrap">
		<header class="w-full md:w-2/5 xl:w-1/2 md:pr-12 lg:pr-20 xl:pr-24 order-1 md:order-1 max-w-2xl">
			<div
				class="z-50 bg-gray-100 bg-opacity-75 bg-opacity-custom lg:min-w-0.7 max-w-xl md:float-right md:text-right leading-loose tracking-tight md:sticky md:top-0 pt-2">
				
<div>
	<h2>
		<a href="https://flwwsg.gitee.io" title="划水王" class="heading font-cursive icon">划水王</a>
	</h2>
</div>
<h1 class="pt-2">基础结构</h1>

<div class="flex flex-wrap justify-end pt-2 "><div class="md:flex-grow-0 font-light">
	
	
	
	
	<a class="post-taxonomy-category text-medium-red-violet-600 hover:text-medium-red-violet-400"
		href='/categories/redis'>redis</a>
	
	
	

	
	&nbsp;&nbsp;
	

	
	
	
	
	<a class="post-taxonomy-tag text-eucalyptus-500"
		href='/tags/redis-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84'>redis 数据结构</a>
	
	
	
</div><time class="text-eucalyptus-500 md:text-right md:flex-grow font-light pl-4"
		datetime="2019-03-26T13:01:01&#43;08:00">2019-3-26 13:01</time>
</div>

<hr />

			</div>
		</header>
		<main role="main" class="w-full md:w-3/5 xl:w-1/2 max-w-3xl order-2 md:order-2 min-h-70vh pt-2 pb-4">
			

<article>
	<section class="mx-auto content">
		<div class="c-rich-text"><h3 id="string-字符串">string 字符串</h3>
<ul>
<li>字符串最大长度512M</li>
<li>如果value是一个整数，还可以自增操作，范围是signed long的最大最小值</li>
</ul>
<h4 id="相关命令">相关命令</h4>
<ul>
<li>set key value</li>
<li>get key</li>
<li>mset key1 value1 key2 value2 &hellip;</li>
<li>mget key1 key2 key3 &hellip;</li>
<li>expire key value (value秒后过期)</li>
<li>setex(原子操作，同时设置过期时间)</li>
<li>setnx(如果不存在就执行set，常用于锁)</li>
<li>incr 自增1</li>
<li>incrby key value, 自增value</li>
</ul>
<h3 id="list-列表">list 列表</h3>
<ul>
<li>相当于链表，而不是数组</li>
<li>list 的插入和删除操作非常快，时间复杂度为 O(1)，但是索引定位很慢，时间复杂度为
O(n)</li>
<li>当列表弹出了最后一个元素之后，该数据结构自动被删除，内存被回收</li>
</ul>
<h4 id="相关命令-1">相关命令</h4>
<ul>
<li>rpush</li>
<li>lpush key value &hellip; (将一个或多个value插到表头, key不存在时，会创建一个空列表)</li>
<li>rpushx</li>
<li>lpushx key value (key不存在时，什么也不做)</li>
<li>llen key (获取列表长度)</li>
<li>lpop</li>
<li>rpop</li>
<li>blpop key &hellip; timeout (它是LPOP命令的阻塞版本，当给定列表内没有任何元素可供弹出的时候，连接将被BLPOP命令阻塞，直到等待超时或发现可弹出元素为止)</li>
<li>brpop key &hellip; timeout</li>
</ul>
<h3 id="set-集合">set 集合</h3>
<ul>
<li>内部键值对是无序、唯一的， 相当于一个特殊的字典，字典中所有的value都是null</li>
</ul>
<h4 id="相关命令-2">相关命令</h4>
<ul>
<li>sadd key member [member &hellip;]</li>
<li>smembers  (获取所有成员)</li>
<li>sismember (判断元素是否是集合的成员)</li>
<li>scard key (返回集合key的基数，即集合中元素的数量)</li>
<li>spop key (移除并返回集合中的一个随机元素)</li>
<li>srandmember key (返回集合中的一个随机元素)</li>
</ul>
<h3 id="hash-哈希">hash 哈希</h3>
<ul>
<li>内部实现结构数组+链表的二维结构， 无序储存</li>
<li>字典的值只能是字符串</li>
<li>不同于java的hashmap 在字典很大时，rehash需要一次性全部rehash。redis为了高性能，不堵塞服务，采用了渐进式rehash。渐进式 rehash 会在 rehash 的同时，保留新旧两个 hash 结构，查询时会同时查询两个
hash 结构，然后在后续的定时任务中以及 hash 的子指令中，循序渐进地将旧 hash 的内容
一点点迁移到新的 hash 结构中。</li>
</ul>
<h4 id="相关命令-3">相关命令</h4>
<ul>
<li>hset</li>
<li>hget</li>
<li>hgetall</li>
<li>hlen</li>
<li>hmset</li>
<li>hincrby</li>
</ul>
<h3 id="zset-有序集">zset 有序集</h3>
<ul>
<li>类似于 Java 的 SortedSet 和 HashMap 的结合体，一方面它是一个 set，保证了内部
value 的唯一性，另一方面它可以给每个 value 赋予一个 score，代表这个 value 的排序权
重。</li>
<li>内部实现采用跳跃列表(跳表)数据结构</li>
<li>跳跃列表采取一个随机策略来决定新元素可以兼职到第几层</li>
</ul>
<h4 id="相关命令-4">相关命令</h4>
<ul>
<li>zadd</li>
<li>zrange</li>
<li>zrevrange</li>
<li>zcard</li>
<li>zscore</li>
<li>zrank</li>
<li>zrangebyscore</li>
<li>zrem</li>
</ul>
<p><em><strong>list/set/hash/zset 这四种数据结构是容器型数据结构, 它们共享两条通用规则</strong></em></p>
<ul>
<li>create if not exists</li>
<li>drop if no elements</li>
</ul>
<p><em><strong>过期时间</strong></em>
redis 所有的数据结构都可以设置过期时间，时间到了，redis会自动删除对应对象，过期是以对象为单位的，比如一个hash结构的过期是整个hash对象的过期，而不是某个key。还有一个需要特别注意的地方是如果一个字符串已经设置了过期时间，然后你调用了
set 方法修改了它，它的过期时间会消失。
ttl 可以查看对象的剩余时间， -1表示永不过期</p>
</div>
	</section>


</article>

		</main>
		<aside role="contentinfo"
			class="w-full md:w-2/5 xl:w-1/2 md:pr-12 lg:pr-20 xl:pr-24 order-4 md:order-3 md:sticky md:bottom-0 self-end max-w-2xl">
			<div class="md:float-right md:text-right leading-loose tracking-tight md:mb-2">
				
	<div class="md:max-w-xs  flex flex-col md:items-end">
	<ul class="font-serif flex-grow-0 flex justify-between flex-wrap md:flex-col">
	
	
	<li class="px-1 md:px-0">
		<a href="/post/" title="Posts page" 
			class="font-medium text-medium-red-violet-600 hover:text-medium-red-violet-400" >
			Posts
		</a>
	</li>
	
	<li class="px-1 md:px-0">
		<a href="/categories/" title="Categories page" >
			Categories
		</a>
	</li>
	
	<li class="px-1 md:px-0">
		<a href="/tags/" title="Tags page" >
			Tags
		</a>
	</li>
	
	
	
	
</ul>
	

<div class="flex flex-wrap-reverse md:justify-end content-end md:content-start justify-start items-start   max-h-16">
	
	<a href='http://github.com/flwwsg' target="_blank" class="github icon pl-1 text-eucalyptus-400 hover:text-java-400" title="github link" rel="noopener"
		aria-label="follow on github——Opens in a new window">
		
		<div class="fill-current h-8 w-8">
			<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
    <g>
        <path fill="none" d="M0 0h24v24H0z"/>
        <path fill-rule="nonzero" d="M5.883 18.653c-.3-.2-.558-.455-.86-.816a50.32 50.32 0 0 1-.466-.579c-.463-.575-.755-.84-1.057-.949a1 1 0 0 1 .676-1.883c.752.27 1.261.735 1.947 1.588-.094-.117.34.427.433.539.19.227.33.365.44.438.204.137.587.196 1.15.14.023-.382.094-.753.202-1.095C5.38 15.31 3.7 13.396 3.7 9.64c0-1.24.37-2.356 1.058-3.292-.218-.894-.185-1.975.302-3.192a1 1 0 0 1 .63-.582c.081-.024.127-.035.208-.047.803-.123 1.937.17 3.415 1.096A11.731 11.731 0 0 1 12 3.315c.912 0 1.818.104 2.684.308 1.477-.933 2.613-1.226 3.422-1.096.085.013.157.03.218.05a1 1 0 0 1 .616.58c.487 1.216.52 2.297.302 3.19.691.936 1.058 2.045 1.058 3.293 0 3.757-1.674 5.665-4.642 6.392.125.415.19.879.19 1.38a300.492 300.492 0 0 1-.012 2.716 1 1 0 0 1-.019 1.958c-1.139.228-1.983-.532-1.983-1.525l.002-.446.005-.705c.005-.708.007-1.338.007-1.998 0-.697-.183-1.152-.425-1.36-.661-.57-.326-1.655.54-1.752 2.967-.333 4.337-1.482 4.337-4.66 0-.955-.312-1.744-.913-2.404a1 1 0 0 1-.19-1.045c.166-.414.237-.957.096-1.614l-.01.003c-.491.139-1.11.44-1.858.949a1 1 0 0 1-.833.135A9.626 9.626 0 0 0 12 5.315c-.89 0-1.772.119-2.592.35a1 1 0 0 1-.83-.134c-.752-.507-1.374-.807-1.868-.947-.144.653-.073 1.194.092 1.607a1 1 0 0 1-.189 1.045C6.016 7.89 5.7 8.694 5.7 9.64c0 3.172 1.371 4.328 4.322 4.66.865.097 1.201 1.177.544 1.748-.192.168-.429.732-.429 1.364v3.15c0 .986-.835 1.725-1.96 1.528a1 1 0 0 1-.04-1.962v-.99c-.91.061-1.662-.088-2.254-.485z"/>
    </g>
</svg>

		</div>
	</a>
	
	<a href='mailto:flwwsg@qq.com' target="_blank" class="mail icon pl-1 text-eucalyptus-400 hover:text-java-400" title="mail link" rel="noopener"
		aria-label="follow on mail——Opens in a new window">
		
		<div class="fill-current h-8 w-8">
			<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
    <g>
        <path fill="none" d="M0 0h24v24H0z"/>
        <path d="M3 3h18a1 1 0 0 1 1 1v16a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1zm17 4.238l-7.928 7.1L4 7.216V19h16V7.238zM4.511 5l7.55 6.662L19.502 5H4.511z"/>
    </g>
</svg>
		</div>
	</a>
	
</div>
	<div class="text-sm text-gray-500 leading-tight a-gray">
		
		<br />
		Built with Hugo and theme <a href="https://github.com/heyeshuang/hugo-theme-tokiwa">Tokiwa</a>. 169 words in this page.
	</div>
</div>

			</div>
		</aside>
		<footer class="w-full md:w-3/5 xl:w-1/2 order-3 max-w-3xl md:order-4 pt-2">
			
<hr class="double-line" />
<div class="flex flex-wrap justify-between pb-2 leading-loose font-serif">
    
    <a class="flex-grow-0" href="/post/redis/redis-%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81/">
        <svg class="fill-current inline-block h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24"
            height="24">
            <path fill="none" d="M0 0h24v24H0z" />
            <path d="M7.828 11H20v2H7.828l5.364 5.364-1.414 1.414L4 12l7.778-7.778 1.414 1.414z" /></svg>
        Redis 分布式锁
    </a>
    
    
    <a class="flex-grow-0" href="/post/javascript/js/">
        js 拾遗
        <svg class="fill-current inline-block h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24"
            height="24">
            <path fill="none" d="M0 0h24v24H0z" />
            <path d="M16.172 11l-5.364-5.364 1.414-1.414L20 12l-7.778 7.778-1.414-1.414L16.172 13H4v-2z" /></svg></a>
    
</div>
<div >



</div>
<hr />
<div class="pb-2">
    
</div>
<hr />

		</footer>
		

<script src="/dist/app.js"></script>


	</div>
</body>

</html>